//
//  HomeCollectionViewLayout.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/21.
//  Copyright © 2020 一介布衣. All rights reserved.
//
#define kBaseLine(a) (CGFloat)a * SCREEN_WIDTH / 375.0
#define COLUMN 4

#import "HomeCollectionViewLayout.h"

@interface HomeCollectionViewLayout()
    @property (nonatomic, assign) CGFloat totalHeight;
    @property (nonatomic, strong) NSMutableArray *attrsArr;
@end

@implementation HomeCollectionViewLayout

-(void)prepareLayout {
    
    [super prepareLayout];
    self.totalHeight = 0;
    NSMutableArray *attributesArr = [NSMutableArray array];
    NSInteger sectionCount = [self.collectionView numberOfSections];
    for (int i = 0; i < sectionCount; i++) {
        NSIndexPath *indexP = [NSIndexPath indexPathWithIndex:i];
        UICollectionViewLayoutAttributes *attr = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexP];
        [attributesArr addObject:attr];
        NSInteger itemCount = [self.collectionView numberOfItemsInSection:i];
        for (int j = 0; j < itemCount; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
            [attributesArr addObject:attrs];
        }
        //UICollectionViewLayoutAttributes *attr1 = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:indexP];
        //[attributesArr addObject:attr1];
    }
    self.attrsArr = [NSMutableArray arrayWithArray:attributesArr];
}

/// contentSize
-(CGSize)collectionViewContentSize {
    return CGSizeMake(self.collectionView.bounds.size.width, self.totalHeight);
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewLayoutAttributes *layoutAttrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    CGFloat height = 0;
    
    if (elementKind == UICollectionElementKindSectionHeader) {
        if (_delegate != nil && [_delegate respondsToSelector:@selector(heightOfSectionHeaderForIndexPath:)]) {
            height = [_delegate heightOfSectionHeaderForIndexPath:indexPath];
        }
    } else {
        if (_delegate != nil && [_delegate respondsToSelector:@selector(heightOfSectionFooterForIndexPath:)]) {
            height = [_delegate heightOfSectionFooterForIndexPath:indexPath];
        }
    }
    layoutAttrs.frame = CGRectMake(0, self.totalHeight, SCREEN_WIDTH, height);
    self.totalHeight += height;
    return layoutAttrs;
}

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.attrsArr;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    [self layoutAttributesLayout:layoutAttributes indexPath:indexPath];
    return layoutAttributes;
}

- (void)layoutAttributesLayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath: (NSIndexPath *) indexPath {
    CGFloat y = self.totalHeight;
    CGFloat offset = 16;
    //NSLog(@"===== section=%i,  item: %i,  row=%i", indexPath.section, indexPath.item, indexPath.row);
    long whichColnums = (indexPath.item) % COLUMN;

    CGFloat width = SCREEN_WIDTH / COLUMN;
    layoutAttributes.frame = CGRectMake(whichColnums * width, y, width, kBaseLine(width));

    if (indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1
        || (indexPath.item + 1) % COLUMN == 0) {
        self.totalHeight += kBaseLine(width + offset);
    }
}

@end
